package com.zkr.sparecost.conterllor;


import com.zkr.sparecost.model.*;
import com.zkr.sparecost.service.SpareCostService;
import com.zkr.util.ResultBuilderVo;
import com.zkr.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * Created by lenovo on 2020/12/18.
 */
@RestController
@CrossOrigin
public class SpareCostConterllor {
    @Autowired
    private SpareCostService spareCostService;

    /**
     * 分页
     * @param spareSelectVo
     * @return
     */
    @PostMapping ("materialCostPrice/queryPage")
    public ResultVo queryPage(@RequestBody SpareSelectVo  spareSelectVo){

        //System.out.println(spareSelectVo);
        return spareCostService.queryPage(spareSelectVo);
    }

   /**
     * 修改启用禁用接口
     * @param spareCostUpdateStatusVo
     * @param bindingResult
     * @return
     */
   @PostMapping("materialCostPrice/updatePriceStatus")
   public ResultVo updateStatus(@RequestBody @Valid SpareCostUpdateStatusVo spareCostUpdateStatusVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return spareCostService.updateStatus(spareCostUpdateStatusVo);
    }


    /**
     * .含税类型下拉框接口
     */
    @GetMapping("materialCostPrice/queryAllTaxType")
    public ResultVo xiala(Integer taxId){

        return spareCostService.queryTaxById(taxId);
    }
    /**
     *
     * 工厂下拉框接口
     */
   @GetMapping("materialCostPrice/queryPlantListByLegalPersonCode")
    public ResultVo gongchang(String legalPersonCode){

        return spareCostService.queryLegalPlantId(legalPersonCode);
    }

    /**
     * 价格 币别 状态 下拉框
     */
    @PostMapping("metadata/getPullDownLists")
    public ResultVo xiala(){

        return spareCostService.xiala();
    }

    /**
     * 备件弹窗
     * material/searchEnabled
     */
    @PostMapping("material/searchEnabled")
    public ResultVo spare(@RequestBody SpareFrameSelectVo spareFrameSelectVo){

        return  spareCostService.querySpare(spareFrameSelectVo);

    }


    /**
     * 添加
     */
    @PostMapping("materialCostPrice/add")
    public ResultVo add(@RequestBody @Valid SpareCostAddVo spareCostAddVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return spareCostService.add(spareCostAddVo);

    }

    /**
     * 同一个时间段内，同法人、同工厂的同一备件不能有两个价格，如果开始时间相同，则以最新提交的覆盖原有的；
     * 添加时间判断
     */

    /**
     * 修改
     */
    @PostMapping("materialCostPrice/update")
    public ResultVo update(@RequestBody @Valid SpareCostUpdate spareCostUpdate, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return spareCostService.update(spareCostUpdate);
    }


}
